#============================================================
HOST ?= aarch64-linux-musleabi-
CC = $(HOST)gcc
AR = $(HOST)ar
LD = $(HOST)ld

CFLAGS = -fPIC -Wall -Os -c
LDFLAGS=-L../output/lib
LIBS = -lpthread -lIOT_comm_shared
# 输出目标名
TARGET = camera_server_shared
# 输出目标存放的路径
TARGET_FOLDER = .
# 源文件路径
SRC_PATH = ./libbuild
# 头文件路径
INCLUDES = ./libbuild
INCLUDES += ../IOT_comm_module/libbuild
# 生成.so文件的目录
LIBS_FOLDER = .
# 定义全局宏
DEFINES = 

#============================================================
OBJ_FOLDER = ./obj

ifeq ($(OS), Windows_NT)
	TARGETSO = $(TARGET).dll
	TARGETA = $(TARGET).lib
else 
	TARGETSO = lib$(TARGET).so
	TARGETA = lib$(TARGET).a
endif

SOURCE_FILES_C= $(wildcard $(SRC_PATH)/*.c)
SOURCE_FILES_CXX= $(wildcard $(SRC_PATH)/*.cpp)
PURE_SOURCE_FILES= $(notdir $(SOURCE_FILES_C)) $(notdir $(SOURCE_FILES_CXX))

INCLUDE_PREFIX=-I
ALL_INCLUDES=$(addprefix $(INCLUDE_PREFIX), $(INCLUDES))

OBJ_FILES_C = $(patsubst %.c,$(OBJ_FOLDER)/%.o,$(PURE_SOURCE_FILES))
OBJ_FILES = $(patsubst %.cpp,$(OBJ_FOLDER)/%.o,$(OBJ_FILES_C))

DEFINE_PREFIX=-D
ALL_DEFINES=$(addprefix $(DEFINE_PREFIX), $(DEFINES))

$(OBJ_FOLDER)/%.o : $(SRC_PATH)/%.c 
	$(CC) $(ALL_DEFINES) $(ALL_INCLUDES) $(LDFLAGS) $(CFLAGS) $< -o $@
$(OBJ_FOLDER)/%.o : $(SRC_PATH)/%.cpp 
	$(CC) $(ALL_DEFINES) $(ALL_INCLUDES) $(LDFLAGS) $(CFLAGS) $< -o $@


.PHONY :all clean lib install service uninstall showlibpary

all:prep lib

prep:
	@if test ! -d $(OBJ_FOLDER); then mkdir $(OBJ_FOLDER); fi	
	@if test ! -d $(TARGET_FOLDER); then mkdir $(TARGET_FOLDER); fi
	@if test ! -d $(LIBS_FOLDER); then mkdir $(LIBS_FOLDER); fi

create:$(OBJ_FILES)	
	$(CC) $(LDFLAGS) -o $(TARGET_FOLDER)/$(TARGET) $(OBJ_FILES) $(LIBS)

show:
	@echo $(OBJ_FILES)

showlibpary:
	@echo 'export PATH=$$PATH:$$PWD'
	@echo 'export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$$PWD'

lib:prep $(OBJ_FILES)
	$(CC) -shared -fPIC $(LDFLAGS) -o $(LIBS_FOLDER)/$(TARGETSO) $(OBJ_FILES) $(LIBS)
	$(AR) rcs $(LIBS_FOLDER)/$(TARGETA) $(OBJ_FILES)

install:lib
	cp $(LIBS_FOLDER)/$(TARGETSO) ../output/lib
	cp ./libbuild/*.h ../output/inc

clean:
	rm -f $(OBJ_FOLDER)/*.o
	rm -f $(TARGET_FOLDER)/$(TARGET) $(LIBS_FOLDER)/$(TARGETSO) $(LIBS_FOLDER)/$(TARGETA) 



